package com.njtoyo.taxi.admin.rest.controller.business.driver;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.rest.presenter.business.driver.DriverListPresenter;
import com.njtoyo.taxi.admin.rest.presenter.business.driver.DriverServiceLogPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.driver.DriverServiceLogQueryWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.driver.DriverServiceLogExportWrapper;
import com.njtoyo.taxi.admin.service.business.driver.DriverServiceLogsService;
import com.taxi.entity.common.RestResult;
import com.njtoyo.taxi.entity.backend.AdminUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.CellStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("business/driver/working")
@Api(tags = {"出车司机名单信息"})
public class DriverServiceLogController {
    @Autowired
    private DriverServiceLogsService driverServiceLogService;


    @GetMapping()
    @ApiOperation(value = "出车司机名单列表", notes = "出车司机名单列表，权限：daily_working_driver_list")
    @PermissionScope(names = {"daily_working_driver_list"})
    RestResult<Page<DriverServiceLogPresenter>> dailyWorkingDriver(@ModelAttribute("admin") AdminUser adminUser,
                                                                   @Valid DriverServiceLogQueryWrapper wrapper) {
        return driverServiceLogService.dailyWorkingDriver(adminUser, wrapper);
    }

    @GetMapping("/export")
    @ApiOperation(value = "出车司机名单列表Excel导出", notes = "出车司机名单列表Excel导出，权限：daily_working_driver_export")
    @PermissionScope(names = {"daily_working_driver_export"})
    void export(@ModelAttribute("admin") AdminUser adminUser,
                @Valid DriverServiceLogQueryWrapper wrapper,
                HttpServletResponse response) throws IOException {
        wrapper.setSize(100000);
        RestResult<Page<DriverServiceLogPresenter>> restResult = driverServiceLogService.dailyWorkingDriver(adminUser, wrapper);
        List<DriverServiceLogPresenter> pages = restResult.getData().getRecords();
        List<Map<String, Object>> rows = new ArrayList<>();
        if (CollUtil.isNotEmpty(pages)) {
            for (DriverServiceLogPresenter presenter : pages) {
                Map<String, Object> row = new HashMap<>();
                row.put("司机编号", presenter.getId());
                row.put("姓名", presenter.getName());
                row.put("手机号", presenter.getMobileNumber());
                row.put("从业资格证", presenter.getQualificationCertificateNumber());
                row.put("车牌", presenter.getPlateNumber());
                row.put("地区", presenter.getCantonName());
                row.put("运力公司", presenter.getCompanyName());
                rows.add(row);
            }
        } else {
            Map<String, Object> row = new HashMap<>();
            row.put("编号", "");
            row.put("姓名", "");
            row.put("手机号", "");
            row.put("从业资格证", "");
            row.put("车牌", "");
            row.put("地区", "");
            row.put("运力公司", "");
            rows.add(row);
        }
        ExcelWriter writer = ExcelUtil.getWriter();
        StyleSet style = writer.getStyleSet();
        CellStyle cellStyle = style.getHeadCellStyle();
        cellStyle.setWrapText(true);
        writer.write(rows, true);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=cars.xls");
        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }
}
